8825. Value of variable 1

 

Find the value of the variable y for a given real value of the variable x.

 

Input. One real number x.

 

Output. Print the value of the variable y rounded to three decimal places.

 

Sample input

Sample output

1

7.286

 

 

SOLUTION

mathematics

 

Algorithm analysis

To solve the problem, compute the value of the given expression.

 

Algorithm implementation

Read the value of the variable x.

 

scanf("%lf", &x);

 

Compute the value of the variable y.

 

y = x * x * x - 5 * x * x / 7 + 9 * x - 3 / x + 1;

 

Print the result rounded to three decimal places.

 

printf("%.3lf\n", y);

 

Java implementation

 

import java.util.*;

 

class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    double x = con.nextDouble();

    double y = x * x * x - 5 * x * x / 7 + 9 * x - 3 / x + 1;

    System.out.printf("%.3f",y);

    con.close();

  }

}

 

Java implementation – class MyDouble

 

import java.util.*;

 

class MyDouble

{

  private double a;

 

  MyDouble(double a)

  {

    this.a = a;

  }

 

  MyDouble Add(MyDouble b)

  {

    return new MyDouble(a + b.a);

  }

 

  MyDouble Add(double b)

  {

    return new MyDouble(a + b);

  }

 

  MyDouble Sub(MyDouble b)

  {

    return new MyDouble(a - b.a);

  }

 

  MyDouble Mult(MyDouble b)

  {

    return new MyDouble(a * b.a);

  }

 

  MyDouble Divide(MyDouble b)

  {

    return new MyDouble(a / b.a);

  }

 

  MyDouble Divide(double b)

  {

    return new MyDouble(a / b);

  }

 

  public String toString()

  {

    return String.format("%.3f", a);

  }

}

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    MyDouble x = new MyDouble(con.nextDouble());

    MyDouble a = x.Mult(x).Mult(x); // x^3

    MyDouble b = new MyDouble(5).Mult(x).Mult(x).Divide(7); // 5 * x * x / 7

    MyDouble c = new MyDouble(9).Mult(x); // 9 * x

    MyDouble d = new MyDouble(3).Divide(x); // 3 / x

 

    MyDouble res = a.Sub(b).Add(c).Sub(d).Add(1);

    System.out.println(res);

    con.close();

  }

}

 

Python implementation

Read the value of the variable x.

 

x = float(input())

 

Compute the value of the variable y.

 

y = x * x * x - 5 * x * x / 7 + 9 * x - 3 / x + 1

 

Print the result.

 

print(y)